<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="toggleCollapse?'64px':'200px'">
        <div @click="toggleCollapse=!toggleCollapse" class="toggle">|||</div>
        <el-menu
          :default-active=activePath
          router
          :collapse-transition="false"
          :collapse="toggleCollapse"
          :unique-opened= "true"
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409eff">
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i class="icon" :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
              <el-menu-item @click="saveNavState('/'+subItem.path)" :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      toggleCollapse:false,
      menuList:[],
      iconsObj:{
        "125":'iconfont icon-user',
        "103":'iconfont icon-tijikongjian',
        "101":'iconfont icon-shangpin',
        "102":'iconfont icon-danju',
        "145":'iconfont icon-baobiao'
      },
      activePath: ''
    }
  },

  methods: {
    logout() {
      sessionStorage.clear()
      this.$router.push('/login')
    },
    saveNavState(path){
    // default-active
      sessionStorage.setItem('activePath',path)
      this.activePath=path
    },
    //获取左侧菜单数据
    async getMenuList(){
      var {data:res} = await this.$http.get('menus')
      // console.log(res)
      if(res.meta.status !=200) {
        return this.$message.error('服务器异常')
      }

      this.menuList = res.data
      // console.log(this.menuList)
      this.$message.success('获取数据成功')
    }
  },

  created() {
    this.activePath = sessionStorage.getItem('activePath')
    this.getMenuList()
  }
}
</script>

<style lang='less' scoped>
.home-container{
  height: 100%;
}
.el-header{
  background: #373d41;
  display: flex;
  justify-content: space-between;
  padding: 10px 5px;
  div{
    display: flex;
    align-items: center;
    span{
      color: #fff;
      font-size: 25px;
      margin-left: 10px;
    }
  }
}
.el-aside{
  background: #333744;
}
.el-main{
  background: #eaedf1;
}
.icon {
  margin-right: 10px;
}
.el-menu{
  border-right: 0;
}
.toggle{
  background-color: #545C64;
  color: #909399;
  font-size: 14px;
  padding: 5px 0;
  text-align: center;
  cursor: pointer;
  user-select: none;
  letter-spacing: 5px;
  font-weight: 700;
}
.toggle:hover{
  background-color: #4C535A;
}
</style>
